package org.rsbot.script.randoms;

import java.util.logging.Logger;
import org.rsbot.gui.AccountManager;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Account;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;

@ScriptManifest(authors={"Holo", "Gnarly", "Salty_Fish", "Pervy Shuya"}, name="BankPin", version=2.3D)
public class BankPins extends Random
{
  public boolean activateCondition()
  {
    return (this.interfaces.get(13).isValid()) || (this.interfaces.getComponent(14, 34).isValid());
  }

  public void enterCode(String paramString)
  {
    if (!this.interfaces.get(13).isValid())
      return;
    RSComponent[] arrayOfRSComponent1 = this.interfaces.get(13).getComponents();
    int i = 0;
    for (int j = 1; j < 5; j++) {
      if (arrayOfRSComponent1[j].containsText("?")) {
        i++;
      }
    }
    i = 4 - i;
    if (!this.interfaces.get(759).isValid())
      return;
    RSComponent[] arrayOfRSComponent2 = this.interfaces.get(759).getComponents();
    for (int k = 0; k < arrayOfRSComponent2.length; k++)
      if (arrayOfRSComponent2[k].containsText(paramString.substring(i, i + 1))) {
        arrayOfRSComponent2[k].doClick();
        sleep(random(500, 1000));
        break;
      }
  }

  public int loop()
  {
    String str = AccountManager.getPin(this.account.getName(), getClass());
    if (this.interfaces.get(14).isValid()) {
      if (this.interfaces.getComponent(14, 34).doClick())
        return -1;
    } else if (this.interfaces.get(13).isValid()) {
      if ((str == null) || (str.length() != 4)) {
        this.log.severe("You must add a bank pin to your account.");
        stopScript(false);
      }
      if ((this.interfaces.get(14).isValid()) || (!this.interfaces.get(13).isValid())) {
        this.interfaces.get(14).getComponent(3).doClick();
        return -1;
      }
      enterCode(str);
      if (this.interfaces.get(211).isValid())
        this.interfaces.get(211).getComponent(3).doClick();
      else if (this.interfaces.get(217).isValid()) {
        sleep(random(10500, 12000));
      }
    }
    return 500;
  }
}